package com.mashibing.conf;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @author liuyno
 * @date 2021/3/7 - 23:05
 * 分布式配置中心代码演示
 */
//@Test
public class TestConfig {
    ZooKeeper zk;


    //获取zk

    @Before
    public void conn() {
        zk = ZKUtil.getZK();
    }

    //使用zk
    @Test
    public void getConf() throws InterruptedException {
        WatchAndCallBack watchAndCallBack = new WatchAndCallBack();
        watchAndCallBack.setZk(zk);
        MyConf myConf = new MyConf();
        watchAndCallBack.setMyConf(myConf);

        watchAndCallBack.await();

        //1、节点存在
        //2、节点不存在
        //3、节点被删除

        while (true) {
            if (myConf.getConf().equals("")) {
                System.out.println("Conf is empty...");
                watchAndCallBack.await();
            } else {
                System.out.println(myConf.getConf());
            }

            Thread.sleep(3000);
        }

    }


    //断开zk
    @After
    public void disConn() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
